package edu.intprog.mail;

import java.io.File;
import java.io.FileInputStream;
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

/**
 * BaseMailer
 * 
 * This class is used as a base for the mail sender classes - this
 * class supply the actual send functionality, while the other classes
 * fill the email data for shipment
 */
public abstract class BaseMailer {
	private String _senderEmail = "";
	private String _recipientEmail = "";
	private static Properties _mailConnectionConf = null;
	
	public BaseMailer(String senderEmail, String recipientEmail) {
		super();
		_senderEmail = senderEmail;
		_recipientEmail = recipientEmail;
	}
	
	/**
	 * buildMessageBody
	 * 
	 * this function create the activation mail body.
	 * 
	 * @param userAddr - user address for activation
	 * @param activationURL - referring servlet url that will handle the activation
	 * @return email body string
	 */
	public abstract String buildMessageBody(String activationURL);

	/**
	 * sendMail
	 * 
	 * this function send the activation mail to the given user address.
	 * activation will be performed on the servlet which is supplied by the url
	 *
	 * @param activationURL - referring servlet url that will handle the activation
	 * 
	 * @return message - if message is empty, sent was successful, false otherwise
	 */
	public synchronized String sendMail(String actiovationURL) {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
 
		Session session = Session.getDefaultInstance(_mailConnectionConf,
				new javax.mail.Authenticator() 
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{ return new PasswordAuthentication(_mailConnectionConf.getProperty("mailUserName"),_mailConnectionConf.getProperty("mailPassword"));	}
		});		
 
		MimeMessage message = new MimeMessage(session);
		String errorMessage = new String();
		try {
			message.setSender(new InternetAddress(_senderEmail));
			message.setSubject(get_subject());
			message.setContent(buildMessageBody(actiovationURL), "text/html");
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(_recipientEmail));
			
			Transport.send(message);
		} catch (Exception e) {
			e.printStackTrace();
			errorMessage = e.getMessage();
		}
		return errorMessage;
	}

	/**
	 * @return mail subject
	 */
	public abstract String get_subject();

	/**
	 * @return sender email address
	 */
	public String get_senderEmail() {
		return _senderEmail;
	}

	/**
	 * @return recipient email address
	 */
	public String get_recipientEmail() {
		return _recipientEmail;
	}
	
	/**
	 * initilizeMailConfigurationFromFile
	 * 
	 * this function initializes the BaseMailer class and link it
	 * with the SMTP server that is configured in the MailConnection.properties
	 * file that is located in the Data folder 
	 * 
	 * @param request - servlet request - required for real path extraction
	 */
	public static synchronized void initilizeMailConfigurationFromFile(HttpServletRequest request)
	{
		
		if(_mailConnectionConf==null) {		
			_mailConnectionConf = new Properties();
			String propFilePath = request.getSession().getServletContext().getRealPath("/")+ File.separator +"Data" + File.separator + "MailConnection.properties";
			try {
				FileInputStream propFileIS = new FileInputStream(propFilePath);
				_mailConnectionConf.load(propFileIS);
			} catch (Exception e) {
				//nothing to do, worst case scenario, mail properties are empty
				//and there will be connection error message for the user
				e.printStackTrace();
			}
		}
	}
}